package util;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;

/**
 * UserRank: tum0r
 * Package Name: util
 * Time: 2019-01-25 Friday 14:07:03
 */
public class SubjectImage {
    public String dealWith() {
        int width = 1000;
        int height = 1000;
        BufferedImage background = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Color color = new Color(0, 191, 255, 100);
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                background.setRGB(i, j, color.getRGB());
            }
        }
        Graphics2D graphics2D = background.createGraphics();
        graphics2D.setColor(new Color(0, 0, 0));
        Font titleFont = new Font("微软雅黑", Font.PLAIN, 50);
        graphics2D.setFont(titleFont);
        FontMetrics fontMetrics = graphics2D.getFontMetrics(titleFont);
        graphics2D.drawString("测试文本", (width - fontMetrics.stringWidth("测试文本")) / 2, height / 10);
        graphics2D.dispose();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            ImageIO.write(background, "png", byteArrayOutputStream);
            byte[] base64 = byteArrayOutputStream.toByteArray();
            String png_base64 = Base64.getEncoder().encodeToString(base64).trim();
            return "data:image/png;base64," + png_base64;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
